package com.aquila.bible.activity;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.ClipboardManager;
import android.text.Html;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.aquila.bible.MyBaseActivity;
import com.aquila.bible.R;
import com.aquila.bible.SearchResultObject;
import com.aquila.bible.mywidget.MyTextScrollView;
import com.aquila.bible.sqlite.CollectionSqlite;
import com.aquila.bible.system.SystemConfig;
import com.aquila.bible.utils.TextReader;
import com.umeng.analytics.MobclickAgent;

public class TextByListActivity extends MyBaseActivity implements OnClickListener{
	
	private TextView titleTextView ;
	private Button prevButton, nextButton ;
	
	private int bookIndex = 1, chapterIndex = 1, totalChapter = 1;
	
	private String chapterContentString ;
	private String title ;
	private String[] sections ;
	private ListAdapter adapter ;
	private LinearLayout readBackgroundLayout ;
	private LinearLayout contentLayout ;
	private SearchResultObject resultObject ;
	private String searchKeyWord = null ;
	private LinearLayout parentLayout ;
	private int textColor ;
	private int[] backgroundImageRes = new int[]{
			R.drawable.readbg_00,
			R.drawable.readbg_01,
			R.drawable.readbg_02,
			R.drawable.readbg_03,
			R.drawable.readbg_04,
			R.drawable.readbg_05,
			R.drawable.readbg_06 ,
			R.drawable.readbg_07 ,
			R.drawable.readbg_08 ,
	} ;
	
	private int[] backcolors ;
	private String backString [];
	private GalleryAdapter galleryAdapter ;
	
	private MyTextScrollView myTextScrollView ;
	private int scrollSpeed = 2 ; // 滚屏的速度
	private boolean isScrolling = false ;
	private int displayTextsize  ;
	private int selectBgIndex ;
	private GridView backGridView ;
	private AlertDialog alertDialog ;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		parentLayout = (LinearLayout) layoutInflater.inflate(R.layout.activity_text_of_listview, null) ;
		setContentView(parentLayout) ;
		
		
		getResFromXML() ;
		
		prevButton.setOnClickListener(this) ;
		nextButton.setOnClickListener(this) ;
		titleTextView.setOnClickListener(this) ;
		
		selectBgIndex = SystemConfig.sp.getInt("selectBg", 0) ;
		displayTextsize = SystemConfig.sp.getInt("displayTextsize_int", 23);
		textColor = backcolors[selectBgIndex] ;
		parentLayout.setBackgroundResource(backgroundImageRes[selectBgIndex]) ;
		
		Bundle bundle = getIntent().getExtras() ;
		if (bundle != null) {
			resultObject = (SearchResultObject) bundle.getSerializable("object") ;
			bookIndex = resultObject.getBookIndex() ;
			chapterIndex = resultObject.getChapterIndex() ;
			totalChapter = resultObject.getChapterCount() ;
			title = resultObject.getBookString() ;
			searchKeyWord = resultObject.getKeyWords() ;
		}
		
		titleTextView.setText(title) ;
		initRes() ;
		adapter = new ListAdapter() ;
		initText() ;
		initPopupMenu() ;
//		initBackStyle() ;
		initBackgroundDialog() ;
		if (SystemConfig.sp.getBoolean("showTextPrompt", true)) {
			showPrompt() ;
		}
	}
	/**
	 * 加载XML资源
	 *2012-8-13 下午4:16:25
	 */
	private void getResFromXML(){
		titleTextView = (TextView) findViewById(R.id.tv_text_title) ;
		prevButton = (Button) findViewById(R.id.btn_chapter_prev) ;
		nextButton = (Button) findViewById(R.id.btn_chapter_next) ;
		contentLayout = (LinearLayout) findViewById(R.id.ll_text_layout) ;
		textColor = getResources().getColor(R.color.section_text_color) ;
		myTextScrollView = (MyTextScrollView) findViewById(R.id.sv_content_layout) ;
//		displayTextsize = getResources().getDimension(R.dimen.content_middle_textsize) ;
		backString = getResources().getStringArray(R.array.read_bg_style_name) ;
		
		backcolors = new int[9] ;
		backcolors[0] = getResources().getColor(R.color.bg_color_style_00) ;
		backcolors[1] = getResources().getColor(R.color.bg_color_style_01) ;
		backcolors[2] = getResources().getColor(R.color.bg_color_style_02) ;
		backcolors[3] = getResources().getColor(R.color.bg_color_style_03) ;
		backcolors[4] = getResources().getColor(R.color.bg_color_style_04) ;
		backcolors[5] = getResources().getColor(R.color.bg_color_style_05) ;
		backcolors[6] = getResources().getColor(R.color.bg_color_style_06) ;
		backcolors[7] = getResources().getColor(R.color.bg_color_style_07) ;
		backcolors[8] = getResources().getColor(R.color.bg_color_style_08) ;
		
		readBackgroundLayout = (LinearLayout) layoutInflater.inflate(R.layout.read_background_select, null) ;
		backGridView = (GridView) readBackgroundLayout.findViewById(R.id.gv_readback_style) ;
		galleryAdapter = new GalleryAdapter() ;
		backGridView.setAdapter(galleryAdapter) ;
		
	
	}
	
//	private void initBackStyle(){
//	}
	
	/**
	 * 操作提示对话框
	 *2012-8-13 下午4:22:02
	 */
	private void showPrompt(){
		
		Builder builder = new Builder(context);
		
		builder.setTitle("操作提示")
		.setMessage("1、点击上面标题栏中的\""+title+"\"可快速选择您要阅读的章节哦。\n" +
				"2、长按经文可对此节经文快捷复制或者收藏。"
				)
		.setPositiveButton("不再提醒", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				SystemConfig.editor.putBoolean("showTextPrompt", false) ;
				SystemConfig.editor.commit() ;
				dialog.dismiss() ;
			}
		})
		.setNegativeButton("我知道了", null)
		.create().show() ;
	}
	
	
	
	
	/***
	 * 
	 * 
	 *2012-8-13 下午4:22:20
	 */
	private void initBackgroundDialog(){
		Builder builder = new Builder(context) ;
		builder.setTitle("背景选择")
		.setView(readBackgroundLayout) ;
		alertDialog = builder.create();
		
		
		backGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
					parentLayout.setBackgroundResource(backgroundImageRes[arg2]) ;
					selectBgIndex = arg2 ;
					textColor = backcolors[arg2] ;
					SystemConfig.editor.putInt("selectBg", arg2) ;
					SystemConfig.editor.commit() ;
					initText() ;
					if (alertDialog.isShowing()) {
                    	alertDialog.dismiss() ;
                    }
            }
		}) ;
	}
	
	/**
	 * 显示文本
	 * 
	 *Administrator
	 *2012-8-6 下午8:50:47
	 */
	private void initText(){
		int len = sections.length ;
		if (len == 0) {
			showToast("无数据", false) ;
			return ;
		}
		resultObject.setChapterIndex(chapterIndex) ;
		
		contentLayout.removeAllViews() ;
		for (int i = 0; i < len; i++) {
			contentLayout.addView(adapter.getView(i, null, null)) ;
		}
	}
	
	
	/**
	 * 文字数据的处理
	 * 
	 *2012-7-30 下午2:55:36
	 */
	private void initRes(){
		chapterContentString = TextReader.readChapter(context, bookIndex, chapterIndex) ;
		if (chapterContentString != null) {
			sections = chapterContentString.split("\n") ;
		}
	}
	
	
	
	
	@Override
	public void onClick(View v) {
		if (v == prevButton) {
			if (chapterIndex == 1) {
				showToast("已经是第一章", false) ;
				return ;
			}
			chapterIndex-- ;
			initRes() ;
			adapter.notifyDataSetChanged() ;
			initText() ;
		}
		else if (v == nextButton) {
			if (chapterIndex >= totalChapter) {
				showToast("已经是最后一章", false) ;
				return ;
			}
			chapterIndex++ ;
			initRes() ;
			adapter.notifyDataSetChanged() ;
			initText() ;
		}
		
		else if (v == titleTextView) {
			Intent intent = new Intent() ;
			intent.setClass(context, ChapterActivity.class) ;
			
			Bundle bundle = new Bundle() ;
			bundle.putSerializable("object", resultObject) ;
			intent.putExtras(bundle) ;
			intent.putExtra("isSelecte", true) ;
			startActivityForResult(intent, 123) ;
		}
		
		else if (v == menuItems[0]) {
//			showToast("字体", false) ;
			showTextSizeDialog() ;
		}
		else if (v == menuItems[1]) { // 阅读背景
			alertDialog.show() ;
		}
		else if (v == menuItems[2]) {	//夜间模式
			if (SystemConfig.isNightType) {
				parentLayout.setBackgroundResource(R.drawable.readbg_00) ;
				textColor = 0xff000000 ;
				SystemConfig.isNightType = false ;
				lightImageView.setImageResource(R.drawable.cartoon_light) ;
				lightTextView.setText("夜间") ;
			}
			else {
				parentLayout.setBackgroundResource(R.drawable.readbg_02) ;
				textColor = 0xff909090;
				SystemConfig.isNightType = true ;
				lightImageView.setImageResource(R.drawable.cartoon_light_increase) ;
				lightTextView.setText("白天") ;
			}
			initText() ;
		}
		else if (v == menuItems[3]) {
			if (!isScrolling) {
				handler.sendEmptyMessage(1) ;
				isScrolling = true ;
				scrollImageView.setImageResource(R.drawable.cartoon_stop) ;
				scrollTextView.setText("停止") ;
			}else {
				handler.sendEmptyMessage(2) ;
				isScrolling = false ;
				scrollImageView.setImageResource(R.drawable.cartoon_play) ;
				scrollTextView.setText("滚动") ;
			}
		}
		else if (v == menuItems[4]) {
			Intent intent = new Intent() ;
    		intent.setClass(this, FeedBackActivity.class) ;
    		startActivity(intent) ;
		}
		else if (v == menuItems[5]) {
//			showToast("很抱歉，此功能正在开发中，我会在下一版本中完善，敬请耐心等待", false) ;
			Intent intent = new Intent() ;
    		intent.setClass(this, CollectionActivity.class) ;
    		startActivity(intent) ;
		}
		else if (v == menuItems[6]) {
			showToast("此功能正在开发中，如果您有什么地方需要个性化设置的，请在反馈中告知我，我会尽快加上。", false) ;
//			Intent intent = new Intent() ;
//    		intent.setClass(this, SettingActivity.class) ;
//    		startActivity(intent) ;
		}
		else if (v == menuItems[7]) {
			Intent intent = new Intent() ;
			intent.setClass(context, AboutActivity.class) ;
			startActivity(intent) ;
			MobclickAgent.onEvent(this, "10") ;
		}
		
		if (pop!= null && pop.isShowing()) {
			pop.dismiss() ;
		}
	}
	
	
	/**
	 * 设置字体大小
	 *2012-8-3 下午3:06:26
	 */
	private void showTextSizeDialog(){
		Builder builder = new Builder(context) ;
		builder.setTitle("字体大小") 
//		.setItems(R.array.textsize_items,new DialogInterface.OnClickListener() {
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//				switch (which) {
//				case 0:
//					displayTextsize = getResources().getDimension(R.dimen.content_small_textsize) ;
//					break;
//				case 1:
//					displayTextsize = getResources().getDimension(R.dimen.content_middle_textsize) ;
//					break;
//				case 2: // 发短信
//					displayTextsize = getResources().getDimension(R.dimen.content_large_textsize) ;
//					break;
//				}
//				SystemConfig.editor.putFloat("displayTextsize", displayTextsize ) ;
//				SystemConfig.editor.commit() ;
//				initText() ;
//			}
//		})
		.setView(getTextsizeView())
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				SystemConfig.editor.putInt("displayTextsize_int", displayTextsize ) ;
				SystemConfig.editor.commit() ;
				initText() ;
			}
		})
		.setNegativeButton("取消", null)		
		.create().show() ;
		
	}
	
	
	private View getTextsizeView(){
		LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.textsize_choice_layout, null) ;
		final TextView textView = (TextView) layout.findViewById(R.id.tv_demo_text) ;
		final TextView sizeNumTextView = (TextView) layout.findViewById(R.id.tv_size_num) ;
		
		Button bigButton = (Button) layout.findViewById(R.id.btn_text_bigger) ;
		Button smallButton = (Button) layout.findViewById(R.id.btn_text_smaller) ;
		final SeekBar seekBar = (SeekBar) layout.findViewById(R.id.seekbar_textsize) ;
		seekBar.setProgress((int)displayTextsize - 8) ;
		seekBar.setMax(30) ;
		sizeNumTextView.setTextSize(displayTextsize) ;
		sizeNumTextView.setText(displayTextsize+"") ;
		bigButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (displayTextsize >= 38) {
					showToast("字体已经调到最大值", false) ;
					return ;
				}
				displayTextsize += 1 ;
				textView.setTextSize(displayTextsize) ;
				sizeNumTextView.setText(displayTextsize+"") ;
				seekBar.setProgress((int)displayTextsize - 8) ;
				
			}
		}) ;
		
		smallButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (displayTextsize <= 8) {
					showToast("字体已经调到最小值", false) ;
					return ;
				}
				
				displayTextsize -= 1 ;
				textView.setTextSize(displayTextsize) ;
				seekBar.setProgress((int)displayTextsize - 8) ;
				sizeNumTextView.setText(displayTextsize+"") ;

			}
		}) ;
		
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				displayTextsize = 8 + progress ;
				textView.setTextSize(displayTextsize) ;
				sizeNumTextView.setText(displayTextsize+"") ;
			}
		}) ;
		
		return layout ;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 123 && data != null) {
			chapterIndex = data.getIntExtra("chapterIndex", 1) ;
			resultObject.setChapterIndex(chapterIndex) ;
			initRes() ;
			adapter.notifyDataSetChanged() ;
			initText() ;
		}
	}
	
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) { // 开启自动滚屏
				myTextScrollView.scrollDown(scrollSpeed) ;
				handler.sendEmptyMessageDelayed(1, 100) ;
			}
			else if (msg.what == 2) { //关闭滚屏
				handler.removeMessages(1) ;
			}
			
		};	
		
		
		
	} ;
	
	private PopupWindow pop ;
	private LinearLayout menuLayout;
	private LinearLayout menuItems[] = new LinearLayout[8] ;
	private ImageView scrollImageView, lightImageView ;
	private TextView scrollTextView, lightTextView ;
	
	/**
     * 初始化菜单的布局
     *2012-7-27 下午3:58:55
     */
    private void initPopupMenu(){
    	menuLayout = (LinearLayout) layoutInflater.inflate(R.layout.menu_at_content_text, null) ;
        pop = new PopupWindow(menuLayout, LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
        pop.update();
        pop.setAnimationStyle(R.style.PopupAnimation) ;
        menuLayout.setFocusableInTouchMode(true) ;
        menuLayout.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_MENU) {
					pop.dismiss() ;
					return true ;
				}
				return false;
			}
		}) ;
        
        menuItems[0] = (LinearLayout) menuLayout.findViewById(R.id.menu_at_content_01) ;
        menuItems[1] = (LinearLayout) menuLayout.findViewById(R.id.menu_at_content_02) ;
        menuItems[2] = (LinearLayout) menuLayout.findViewById(R.id.menu_at_content_03) ;
        menuItems[3] = (LinearLayout) menuLayout.findViewById(R.id.menu_at_content_04) ;
        menuItems[4] = (LinearLayout) menuLayout.findViewById(R.id.menu_at_content_05) ;
        menuItems[5] = (LinearLayout) menuLayout.findViewById(R.id.menu_at_content_06) ;
        menuItems[6] = (LinearLayout) menuLayout.findViewById(R.id.menu_at_content_07) ;
        menuItems[7] = (LinearLayout) menuLayout.findViewById(R.id.menu_at_content_08) ;
        
        scrollImageView = (ImageView) menuLayout.findViewById(R.id.iv_menu_at_content_04) ;
        scrollTextView = (TextView) menuLayout.findViewById(R.id.tv_menu_at_content_04) ;
        
        lightImageView = (ImageView) menuLayout.findViewById(R.id.iv_menu_at_content_03) ;
        lightTextView = (TextView) menuLayout.findViewById(R.id.tv_menu_at_content_03) ;
        
        for (int i = 0; i < menuItems.length; i++) {
//        	if (i < 4) {
//				menuItems[i].setVisibility(View.GONE) ;
//				continue ;
//			}
			menuItems[i].setOnClickListener(this) ;
		}
    }
	
	
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
    	if (pop != null) {
    		if (pop.isShowing()) {
				pop.dismiss() ;
			}
    		else {
    			pop.showAtLocation(menuLayout, Gravity.BOTTOM, 0, 0) ;
			}
    	}
    
    	return false ; //super.onMenuOpened(featureId, menu);
    }
    
	
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add("item") ;
    	
    	return super.onCreateOptionsMenu(menu);
    }
	
	class ListAdapter extends BaseAdapter{
		
		@Override
		public int getCount() {
			if (sections != null) {
				return sections.length ;
			}
			return 0;
		}
		
		@Override
		public Object getItem(int position) {
			return position;
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		Pattern pattern  ;
		
		
		@Override
		public View getView(final int position, View v, ViewGroup vg) {
			LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.adapter_text_content_of_listvew, null) ;
			TextView sectionNumTextView = (TextView) layout.findViewById(R.id.tv_chapter_num) ;
			TextView sectionContentTextView = (TextView) layout.findViewById(R.id.tv_chapter_content_text) ;
			sectionContentTextView.setTextColor(textColor) ;
			
			final String temp = sections[position] ;
			pattern = Pattern.compile(SystemConfig.regex) ;
			Matcher matcher = pattern.matcher(temp) ;
			String val = null ;
			String content ;
			int len = 0  ;
			if (matcher.find()) {
				val = matcher.group() ;
				len = val.length() ;
				if (position == 0) { //去掉首行多空一格字符
					len++ ;
				}
			}
			content = temp.substring(len+1) ;
			
			sectionNumTextView.setText(val);
			if (searchKeyWord != null) {
				content = content.replace(searchKeyWord, "<font color=#11ca41>" + searchKeyWord +"</font>") ;
			}
			sectionContentTextView.setTextSize(displayTextsize) ;
			
			sectionContentTextView.setText(Html.fromHtml(content)) ;
			layout.setOnLongClickListener(new OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View v) {
					showDialog(temp, position) ;
					return false;
				}
			}) ;
			return layout;
		}
	}
	
	
	
	
	
	
	/**
	 * 显示长按经文的操作对话框
	 * @param content
	 *Administrator
	 *2012-8-6 下午8:02:07
	 */
	private void showDialog(final String content, final int position){
		Builder builder = new Builder(context) ;
		builder.setTitle("选择操作")
		.setItems(R.array.action_type,new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					showToast("已复制到剪贴板", false) ;
					ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE) ;
					cm.setText(content) ;
					break;
				case 1:
					CollectionSqlite sqlite = new CollectionSqlite(context) ;
					SearchResultObject object = new SearchResultObject() ;
					object.setBookIndex(resultObject.getBookIndex()) ;
					object.setBookShortString(resultObject.getBookShortString()) ;
					object.setBookString(resultObject.getBookString()) ;
					object.setChapterCount(resultObject.getChapterCount()) ;
					object.setChapterIndex(resultObject.getChapterIndex()) ;
					object.setContentString(content) ;
					object.setId(resultObject.getId()) ;
					object.setKeyWords(" "); //+resultObject.getKeyWords()) ;
					object.setSectionCount(sections.length) ;
					object.setSectionIndex(position + 1) ;
					object.setTimeString(SystemConfig.sdf_2.format(Calendar.getInstance().getTime())) ;
					
					sqlite.open() ;
					
					boolean flag = sqlite.insertData(object) ; 
					if (flag) {
						showToast("添加书签成功",false) ;
                    }
					else  {
						showToast("您已经添加了此节经文",false) ;
					}
					sqlite.close() ;
					
					
					break;
				case 2: // 发短信
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.putExtra("sms_body", content);
					intent.setType("vnd.android-dir/mms-sms");
					startActivity(intent);
					break;

				default:
					break;
				}
			}
		})
		.create().show() ;
	}
	
	
	
	class GalleryAdapter extends BaseAdapter{

		@Override
        public int getCount() {
	        // TODO Auto-generated method stub
	        return backgroundImageRes.length;
        }

		@Override
        public Object getItem(int position) {
	        // TODO Auto-generated method stub
	        return position;
        }

		@Override
        public long getItemId(int position) {
	        // TODO Auto-generated method stub
	        return position;
        }

		@Override
        public View getView(int position, View convertView, ViewGroup parent) {
			LinearLayout bgLayout = (LinearLayout) layoutInflater.inflate(R.layout.read_bg_gallery_item_layouty, null) ;
			TextView bgPrompTextView = (TextView) bgLayout.findViewById(R.id.tv_bg_name) ;
			
			bgLayout.setBackgroundResource(backgroundImageRes[position]) ;
			
			bgPrompTextView.setText(backString[position]) ;
			bgPrompTextView.setTextColor(backcolors[position]) ;
			
			return bgLayout;
        }
		
	}
	
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean isVolCol = true ; //SystemConfig.sp.getBoolean("settingVolume", true) ; 
		if (KeyEvent.KEYCODE_VOLUME_DOWN== keyCode) {
			if (isVolCol) {
				myTextScrollView.scrollDown(scrollSpeed) ;
			}
			return true ;
		}
		else if (KeyEvent.KEYCODE_VOLUME_UP == keyCode) {
			if (isVolCol) {
				myTextScrollView.scrollUp(scrollSpeed) ;
			}
			return true ;
		}
		else{
			return super.onKeyDown(keyCode, event);
		}
	}
	
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		String readTime = "<font color=#122cc4>" + SystemConfig.sdf_2.format(Calendar.getInstance().getTime()) +"</font>" ;
		SystemConfig.editor.putString("lastReadTime", readTime) ;
		
		String readInfo = "  阅读到: <font color=#1f9c3f>"+resultObject.getBookString() +"</font>第 <font color=#ab113c>"+resultObject.getChapterIndex() +"</font> 章";
		SystemConfig.editor.putString("readInfo", readInfo) ;
		SystemConfig.editor.commit() ;
	}
	
}
